// 用户相关API
import { request, API_BASE_URL } from '@/utils/http';
import type { RegisterParams } from '../types';

// 重置密码
export const resetPassword = async (data: { phone: string; password: string; smsCode: string }) => {
  try {
    return await request(`${API_BASE_URL}/user/updatePassword`, {
      method: 'POST',
      data: data,
    });
  } catch (error: any) {
    console.error('重置密码请求失败:', error);
    // 如果是401错误，直接抛出让拦截器处理token刷新
    if (error?.response?.status === 401 || error?.status === 401) {
      throw error;
    }
    // 返回错误响应
    throw error;
  }
};

// 用户注册
export const register = async (data: RegisterParams) => {
  try {
    return await request(`${API_BASE_URL}/user/register`, {
      method: 'POST',
    data: data,
    });
  } catch (error: any) {
    console.error('注册请求失败:', error);
    // 如果是401错误，直接抛出让拦截器处理token刷新
    if (error?.response?.status === 401 || error?.status === 401) {
      throw error;
    }
    // 如果后端服务未启动，返回mock数据
    return {
      code: 200,
      message: '注册成功 (Mock数据)',
      data: {
        userId: 'user_' + new Date().getTime(),
        ...data
      }
    };
  }
};


// 成员数据类型
export interface Member {
  id: string
  name: string
  position: string
  avatar: string
  isManager: boolean
  departmentId: string
}

// 用户信息数据类型
export interface UserInfo {
  id: number
  name: string
  phone: string
  admin: number
  department: number
  ceo: number
  hr: number
  url: string
  position: string
  createDate:string
}

// 根据用户ID查询用户信息
export const getUserById = async (id: string): Promise<{ code: number; msg: string; data: UserInfo }> => {
  try {
    return await request(`${API_BASE_URL}/OA/query/userById?id=${id}`, {
      method: 'POST',
    });
  } catch (error: any) {
    console.error('查询用户信息失败:', error);
    // 如果是401错误，直接抛出让拦截器处理token刷新
    if (error?.response?.status === 401 || error?.status === 401) {
      throw error;
    }
    // 返回错误响应
    throw new Error('查询用户信息失败');
  }
};

// 获取部门成员列表
export const getDepartmentMembers = (departmentId: string) => {
  // 导入图片
  const pictureImg = '/src/assets/homeOther/picture.png'
  const pictureOneImg = '/src/assets/homeOther/sm_picture.png'
  const pictureTwoImg = '/src/assets/homeOther/picture_two.png'
  const rectangle122Img = '/src/assets/homeOther/Rectangle 122@2x.png'
  
  // mock数据
  const mockMembers: Record<string, Member[]> = {
    'hr': [
      {
        id: '1',
        name: '名字',
        position: '职位',
        avatar: pictureImg,
        isManager: false,
        departmentId: 'hr'
      },
      {
        id: '2',
        name: '员工A',
        position: '职位',
        avatar: pictureOneImg,
        isManager: false,
        departmentId: 'hr'
      },
      {
        id: '3',
        name: '名字',
        position: '职位',
        avatar: pictureTwoImg,
        isManager: false,
        departmentId: 'hr'
      },
      {
        id: '4',
        name: '名字',
        position: '职位',
        avatar: pictureImg,
        isManager: false,
        departmentId: 'hr'
      },
      {
        id: '5',
        name: '名字',
        position: '职位',
        avatar: rectangle122Img,
        isManager: false,
        departmentId: 'hr'
      }
    ],
    'finance': [
      {
        id: '6',
        name: '财务主管',
        position: '主管',
        avatar: pictureImg,
        isManager: false,
        departmentId: 'finance'
      },
      {
        id: '7',
        name: '会计',
        position: '会计师',
        avatar: pictureOneImg,
        isManager: false,
        departmentId: 'finance'
      }
    ],
    'rd': [
      {
        id: '8',
        name: '技术总监',
        position: '总监',
        avatar: pictureTwoImg,
        isManager: false,
        departmentId: 'rd'
      },
      {
        id: '9',
        name: '前端工程师',
        position: '工程师',
        avatar: pictureImg,
        isManager: false,
        departmentId: 'rd'
      }
    ]
  }
  
  return new Promise<{ code: number; message: string; data: Member[] }>((resolve) => {
    setTimeout(() => {
      const members = mockMembers[departmentId] || []
      resolve({
        code: 200,
        message: '获取成功',
        data: members
      })
    }, 500) // 模拟网络延迟
  })
  
  // 真实接口调用方式
  // return request({
  //   url: `/api/department/${departmentId}/members`,
  //   method: 'GET'
  // })
}
